package xyz.xpman.prisonerfitness.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import java.util.HashMap;
import java.util.Map;

import xyz.xpman.prisonerfitness.R;
import xyz.xpman.prisonerfitness.config.Constant;
import xyz.xpman.prisonerfitness.entity.Mappings;
import xyz.xpman.prisonerfitness.entity.Moves;
import xyz.xpman.prisonerfitness.entity.TrainingStandard;
import xyz.xpman.prisonerfitness.util.ScreenUtil;

@RequiresApi(api = Build.VERSION_CODES.M)
public class TrainingStandardActivity extends Activity {
    private int selectStandard = R.id.easy_standard_wrap;
    private int defaultBgColor;
    private int selectBgColor;

    private Moves move;
    private TrainingStandard trainingStandard;
    private TrainingStandard.IStandard standard;
    private final Map<Integer, TrainingStandard.IStandard> standardMapping = new HashMap<>();

    private TextView movesName;
    private TextView easyStandard;
    private TextView mediumStandard;
    private TextView upgradeStandard;

    private ViewGroup customStandardWrap;
    private TextView customStandardGroup;
    private TextView customStandardTimes;

    private View saveCustom;
    private ViewGroup customStandardSettingWrap;
    private TextView settingCustomStandardGroup;
    private TextView settingCustomStandardTimes;



    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_training_standard);

        defaultBgColor = this.getColor(R.color.white);
        selectBgColor = this.getColor(R.color.light_blue_A400);

        initPopWindow();
        initData();
        initView();
        initEvent();
    }

    private void initPopWindow() {
        // 设置窗口宽度，高度则自适应
        Point windowsSize = ScreenUtil.getWindowsSize(this);
        int width = (int)((float) windowsSize.x * 0.85);
        getWindow().setLayout(width, LinearLayout.LayoutParams.WRAP_CONTENT);
    }


    @RequiresApi(api = Build.VERSION_CODES.N)
    private void initData() {
        final String id = (String) getIntent()
                .getSerializableExtra(Constant.EXTRA_MOVES_ID);

        move = Mappings.getMove(id);

        TrainingStandard standard = move.getTraining().getStandard();
        standardMapping.put(R.id.easy_standard_wrap, standard.getEasyStandard());
        standardMapping.put(R.id.medium_standard_wrap, standard.getMediumStandard());
        standardMapping.put(R.id.upgrade_standard_wrap, standard.getUpgradeStandard());
        standardMapping.put(R.id.custom_standard_wrap, standard.getCustomStandard());

        trainingStandard = standard;
        this.standard = standard.getEasyStandard();
    }


    @RequiresApi(api = Build.VERSION_CODES.M)
    private void initView() {
        movesName = findViewById(R.id.moves_name);

        easyStandard = findViewById(R.id.easy_standard);
        mediumStandard = findViewById(R.id.medium_standard);
        upgradeStandard = findViewById(R.id.upgrade_standard);

        customStandardGroup = findViewById(R.id.custom_group);
        customStandardTimes = findViewById(R.id.custom_times);
        settingCustomStandardGroup = findViewById(R.id.setting_custom_group);
        settingCustomStandardTimes = findViewById(R.id.setting_custom_times);

        saveCustom = findViewById(R.id.save_custom_standard);

        // selectedStanderWrap
        findViewById(selectStandard).setBackgroundColor(selectBgColor);


        View easyStandardWrap = findViewById(R.id.easy_standard_wrap);
        View mediumStandardWrap = findViewById(R.id.medium_standard_wrap);
        View upgradeStandardWrap = findViewById(R.id.upgrade_standard_wrap);

        customStandardWrap = findViewById(R.id.custom_standard_wrap);
        customStandardSettingWrap = findViewById(R.id.custom_standard_setting_wrap);
        customStandardSettingWrap.setVisibility(View.GONE);


        View.OnClickListener selectStandard = view -> {
            if (View.VISIBLE == customStandardSettingWrap.getVisibility()) {
                saveCustom.callOnClick();
            }

            easyStandardWrap.setBackgroundColor(defaultBgColor);
            mediumStandardWrap.setBackgroundColor(defaultBgColor);
            upgradeStandardWrap.setBackgroundColor(defaultBgColor);
            customStandardWrap.setBackgroundColor(defaultBgColor);

            view.setBackgroundColor(selectBgColor);
            this.selectStandard = view.getId();
            this.standard = standardMapping.get(view.getId());
        };


        easyStandardWrap.setOnClickListener(selectStandard);
        mediumStandardWrap.setOnClickListener(selectStandard);
        upgradeStandardWrap.setOnClickListener(selectStandard);

        customStandardWrap.setOnClickListener(view-> {
            selectStandard.onClick(view);

            // process
            customStandardWrap.setVisibility(View.GONE);
            customStandardSettingWrap.setVisibility(View.VISIBLE);
        });
    }

    private void initEvent() {
        findViewById(R.id.start).setOnClickListener(view -> {
            Intent intent = new Intent(this, TrainingActivity.class);
            intent.putExtra(Constant.EXTRA_MOVES_ID, move.getId());
            intent.putExtra(Constant.EXTRA_STANDARD, standard);
            startActivity(intent);
        });


        saveCustom.setOnClickListener(view-> {
            parseCustomStandard();
            processCustomStandard();

            saveCustomStandard();

            customStandardWrap.setVisibility(View.VISIBLE);
            customStandardSettingWrap.setVisibility(View.GONE);
        });
    }



    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



    private void parseCustomStandard() {
        int group = Integer.parseInt(String.valueOf(settingCustomStandardGroup.getText()));
        int times = Integer.parseInt(String.valueOf(settingCustomStandardTimes.getText()));

        getCustomStandard().setGroup(group).setTimes(times);

        // TODO: 2021/6/3 save user preference
    }

    private TrainingStandard.CustomStandard getCustomStandard() {
        return (TrainingStandard.CustomStandard) standardMapping.get(R.id.custom_standard_wrap);
    }

    private void saveCustomStandard() {
        // todo
    }


    private void processCustomStandard() {
        TrainingStandard.Standard customStandard = trainingStandard.getCustomStandard();

        final String groups = String.valueOf(customStandard.getGroups());
        final String times = String.valueOf(customStandard.getTimes());

        customStandardGroup.setText(groups);
        customStandardTimes.setText(times);

//        settingCustomStandardGroup.setText(groups);
//        settingCustomStandardTimes.setText(times);
    }



    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



    @Override
    protected void onResume() {
        super.onResume();

        movesName.setText(move.getTitle());

        TrainingStandard standard = trainingStandard;

        easyStandard.setText(standard.getEasyStandard().toString());
        mediumStandard.setText(standard.getMediumStandard().toString());
        upgradeStandard.setText(standard.getUpgradeStandard().toString());

        processCustomStandard();
    }

}